package Com.Ring.TradingMobile.Lib;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import Com.Ring.TradingMobile.R;
import Com.Ring.TradingMobile.Service.WebService;
import android.app.Activity;
import android.util.Log;
import android.widget.TextView;

public class Common {

	public static final String KeyIntentThiTruong = "ThiTruong";
	public static final String DanhMucRoot = "Main";
	public static String TAG = "Common";
	private static Thread myThread = null;
	private static boolean IsRunningTimer;
	public static Activity activity;

	public static enum MARKET {
		HOSE, HNX, UPCOM
	}

	public static class ACCOUNT {
		public static class ACCOUNT_TYPE {
			public static final int NORMAL = 0;
			public static final int MARGIN = 1;
			public static final int ORTHER = 2;
		}

		public static int GetAccountType(String accountNo) {
			int returnValue = ACCOUNT_TYPE.ORTHER;
			if (accountNo != null && accountNo != "") {
				int accountType = Integer.parseInt(accountNo
						.substring(accountNo.length() - 1));
				if (accountType == 1) {
					returnValue = ACCOUNT_TYPE.NORMAL;
				} else if (accountType == 6) {
					returnValue = ACCOUNT_TYPE.MARGIN;
				}
			}
			return returnValue;
		}
	}

	public static int ConvertToIntMarket(MARKET market) {
		switch (market) {
		case HOSE:
			return 1;
		case HNX:
			return 2;
		case UPCOM:
			return 3;
		default:
			return 1;
		}
	}

	public static MARKET ConvertToEnumMarket(int marketId) {
		switch (marketId) {
		case 1:
			return MARKET.HOSE;
		case 2:
			return MARKET.HNX;
		case 3:
			return MARKET.UPCOM;
		default:
			return MARKET.HOSE;
		}
	}

	public static String FormatNumber(float number) {
		NumberFormat formatter = new DecimalFormat("##,##0.##");
		return formatter.format(number);
	}

	public static String FormatNumber(double number) {
		NumberFormat format = new java.text.DecimalFormat("##,##0.##");
		return format.format(number);
	}

	public static String FormatShortNumber(float number) {
		float Bilion = 1000000000;
		float Milion = 1000000;
		float Thousand = 1000;
		if (Math.abs(number) >= Bilion) {
			float dbTemp = (float) number / Bilion;
			return FormatNumber(dbTemp) + " T";
		} else if (Math.abs(number) >= Milion) {
			float dbTemp = (float) number / Milion;
			return FormatNumber(dbTemp) + " Tr";
		} else if (Math.abs(number) >= Thousand) {
			float dbTemp = (float) number / Thousand;
			return FormatNumber(dbTemp) + " N";
		} else {
			return FormatNumber(number);
		}
	}
	
	public static String FormatShortNumber(double number)
    {
		float Bilion = 1000000000;
		float Milion = 1000000;
		float Thousand = 1000;
		if (Math.abs(number) >= Bilion) {
			double dbTemp = number / Bilion;
			return FormatNumber(dbTemp) + " T";
		} else if (Math.abs(number) >= Milion) {
			double dbTemp = number / Milion;
			return FormatNumber(dbTemp) + " Tr";
		} else if (Math.abs(number) >= Thousand) {
			double dbTemp = number / Thousand;
			return FormatNumber(dbTemp) + " N";
		} else {
			return FormatNumber(number);
		}
    }

	public static String ConvertToDateStrUs(String str) {
		String result = "";
		if (!str.equals("")) {
			result = str.substring(6, str.length() - 2);
			result = android.text.format.DateFormat.format("yyyy-MM-dd",
					Long.parseLong(result)).toString();
		}
		return result;
	}

	public static String ConvertToDateStrVi(String str) {
		String result = "";
		if (!str.equals("")) {
			result = str.substring(6, str.length() - 2);
			result = android.text.format.DateFormat.format("dd-MM-yyyy",
					Long.parseLong(result)).toString();
		}
		return result;
	}

	public static String ConvertToDateStrInit(String str) {
		String result = "";
		if (!str.equals("")) {
			result = str.substring(6, str.length() - 2);
			result = android.text.format.DateFormat.format("yyyyMMdd",
					Long.parseLong(result)).toString();
		}
		return result;
	}

	public static int GetMarketStatus(MARKET marketEnum, String status) {
		switch (marketEnum) {
		case HOSE:
			if (status.toUpperCase().equals("P"))
				return Com.Ring.TradingMobile.R.string.strSession1;
			else if (status.toUpperCase().equals("O"))
				return Com.Ring.TradingMobile.R.string.strSession2;
			else if (status.toUpperCase().equals("A"))
				return Com.Ring.TradingMobile.R.string.strSession3;
			else if (status.toUpperCase().equals("C"))
				return Com.Ring.TradingMobile.R.string.strAgreement;
			else if (status.toUpperCase().equals("K"))
				return Com.Ring.TradingMobile.R.string.strClose;
		case HNX:
			if (status.toUpperCase().equals("O"))
				return Com.Ring.TradingMobile.R.string.strOpen;
			else if (status.toUpperCase().equals("C"))
				return Com.Ring.TradingMobile.R.string.strClose;
		case UPCOM:
			if (status.toUpperCase().equals("O")
					|| status.toUpperCase().equals("U"))
				return Com.Ring.TradingMobile.R.string.strOpen;
			else if (status.toUpperCase().equals("H")
					|| status.toUpperCase().equals("C"))
				return Com.Ring.TradingMobile.R.string.strClose;

		default:
			return Com.Ring.TradingMobile.R.string.strUnvailiable;
		}
	}

	public static void StartTimer(Runnable runable, Activity act) {
		IsRunningTimer = true;
		activity = act;
		if (myThread != null && myThread.isAlive()) {
			myThread.stop();
		}
		myThread = new Thread(runable);
		myThread.start();
	}

	public static void StopTimer() {
		IsRunningTimer = false;
	}

	public static void doWork() {

		activity.runOnUiThread(new Runnable() {
			public void run() {
				try {
					TextView txtCurrentTime = (TextView) activity
							.findViewById(Com.Ring.TradingMobile.R.id.txtTimer);
					DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
					Date date = new Date();
					txtCurrentTime.setText(dateFormat.format(date));
				} catch (Exception e) {
				}
			}
		});
	}

	public static class CountDownRunner implements Runnable {
		// @Override
		public void run() {
			while (IsRunningTimer) {
				try {
					doWork();
					Thread.sleep(1000);

				} catch (InterruptedException e) {
					Thread.currentThread().interrupt();
				} catch (Exception e) {
				}
			}
		}
	}
	
	public static OrderStatus_Code GetOrderStatus_Code(int code) {
		return OrderStatus_Code.intToEnumCode(code);
	}

	public static RET_CODE GetRetCode(int inputRetCode) {
		return RET_CODE.intToEnumCode(inputRetCode);
	}

	public static int GetRetCodeErrorMsg(int inputRetCode) {
		int int_mess = 0;
		RET_CODE reCode = RET_CODE.intToEnumCode(inputRetCode);

		switch (reCode) {
		case SYSTEM_ERROR:
			int_mess = Com.Ring.TradingMobile.R.string.RET_CODE_SYSTEM_ERROR;
			break;
		case NO_EXISTED_DATA:
			int_mess = Com.Ring.TradingMobile.R.string.RET_CODE_NO_EXISTED_DATA;
			break;
		case EXISTED_DATA:
			int_mess = Com.Ring.TradingMobile.R.string.RET_CODE_EXISTED_DATA;
			break;
		case INCORRECT_USER_PASSWORD:
			int_mess = Com.Ring.TradingMobile.R.string.RET_CODE_INCORRECT_USER_PASSWORD;
			break;
		case INCORRECT_PASSWORD:
			int_mess = Com.Ring.TradingMobile.R.string.RET_CODE_INCORRECT_PASSWORD;
			break;
		case PASSWORD_NOT_MATCH:
			int_mess = Com.Ring.TradingMobile.R.string.RET_CODE_PASSWORD_NOT_MATCH;
			break;
		case PASSWORD_EMPTY:
			int_mess = Com.Ring.TradingMobile.R.string.RET_CODE_PASSWORD_EMPTY;
			break;
		case PASSWORD_INACTIVED:
			int_mess = Com.Ring.TradingMobile.R.string.RET_CODE_PASSWORD_INACTIVED;
			break;
		case ACCOUNT_INACTIVE:
			int_mess = Com.Ring.TradingMobile.R.string.RET_CODE_ACCOUNT_INACTIVE;
			break;
		case SEND_WARNING_SMS:
			int_mess = Com.Ring.TradingMobile.R.string.RET_CODE_SEND_WARNING_SMS;
			break;
		case SHOW_CAPTCHA:
			int_mess = Com.Ring.TradingMobile.R.string.RET_CODE_SHOW_CAPTCHA;
			break;
		case ACCOUNT_LOCKED:
			int_mess = Com.Ring.TradingMobile.R.string.RET_CODE_ACCOUNT_LOCKED;
			break;
		case ERROR_NOT_STOCK_AVAILABLE:
			int_mess = Com.Ring.TradingMobile.R.string.RET_CODE_ERROR_NOT_STOCK_AVAILABLE;
			break;
		case ERROR_NOT_CASH_AVAILABLE:
			int_mess = Com.Ring.TradingMobile.R.string.RET_CODE_ERROR_NOT_CASH_AVAILABLE;
			break;
		case IS_VALID:
			int_mess = Com.Ring.TradingMobile.R.string.RET_CODE_IS_VALID;
			break;
		case NOT_ALLOW:
			int_mess = Com.Ring.TradingMobile.R.string.RET_CODE_NOT_ALLOW;
			break;
		case INCORRECT_PIN:
			int_mess = Com.Ring.TradingMobile.R.string.RET_CODE_INCORRECT_PIN;
			break;
		case ERROR_GW_NOT_CONNECTED:
			int_mess = Com.Ring.TradingMobile.R.string.RET_CODE_ERROR_GW_NOT_CONNECTED;
			break;
		case ERROR_GW_NOT_SEND:
			int_mess = Com.Ring.TradingMobile.R.string.RET_CODE_ERROR_GW_NOT_SEND;
			break;
		case INCORRECT_FORMAT:
			int_mess = Com.Ring.TradingMobile.R.string.RET_CODE_INCORRECT_FORMAT;
			break;
		case ERROR_INVALID_CASH_ADVANCE:
			int_mess = Com.Ring.TradingMobile.R.string.RET_CODE_ERROR_INVALID_CASH_ADVANCE;
			break;
		case ERROR_NOT_ENOUGH_CASH_TO_ADVANCE:
			int_mess = Com.Ring.TradingMobile.R.string.RET_CODE_ERROR_NOT_ENOUGH_CASH_TO_ADVANCE;
			break;
		case ERROR_CANNOT_ADVANCE_IN_DUE_DATE:
			int_mess = Com.Ring.TradingMobile.R.string.RET_CODE_ERROR_CANNOT_ADVANCE_IN_DUE_DATE;
			break;
		case ERROR_CANNOT_ADVANCE_OUTOF_TIME:
			int_mess = Com.Ring.TradingMobile.R.string.RET_CODE_ERROR_CANNOT_ADVANCE_OUTOF_TIME;
			break;
		case ERROR_CANNOT_ADVANCE_FOR_TRADING_AT_AFTERNOON:
			int_mess = Com.Ring.TradingMobile.R.string.RET_CODE_ERROR_CANNOT_ADVANCE_FOR_TRADING_AT_AFTERNOON;
			break;
		case ERROR_CANNOT_CANCEL_IN_PROCESSING:
			int_mess = Com.Ring.TradingMobile.R.string.RET_CODE_ERROR_CANNOT_CANCEL_IN_PROCESSING;
			break;
		case ERROR_CANNOT_CANCEL_ADVANCE_FINISHED:
			int_mess = Com.Ring.TradingMobile.R.string.RET_CODE_ERROR_CANNOT_CANCEL_ADVANCE_FINISHED;
			break;
		case ERROR_CANNOT_CANCEL_ADVANCE_REFJECTED:
			int_mess = Com.Ring.TradingMobile.R.string.RET_CODE_ERROR_CANNOT_CANCEL_ADVANCE_REFJECTED;
			break;
		case ERROR_CANNOT_CANCEL_ADVANCE_CANCELED:
			int_mess = Com.Ring.TradingMobile.R.string.RET_CODE_ERROR_CANNOT_CANCEL_ADVANCE_CANCELED;
			break;
		case RANGE_OVERLAP:
			int_mess = Com.Ring.TradingMobile.R.string.RET_CODE_RANGE_OVERLAP;
			break;
		case OUT_OF_NEXT_DAY_ORDER_TIME:
			int_mess = Com.Ring.TradingMobile.R.string.RET_CODE_OUT_OF_NEXT_DAY_ORDER_TIME;
			break;
		case ADVANCE_ORDER_STATUS_INCORRECT_STATE:
			int_mess = Com.Ring.TradingMobile.R.string.RET_CODE_ADVANCE_ORDER_STATUS_INCORRECT_STATE;
			break;
		case ERROR_NOT_SAME_ACCOUNT:
			int_mess = Com.Ring.TradingMobile.R.string.RET_CODE_ERROR_NOT_SAME_ACCOUNT;
			break;
		case ERROR_REQUEST_AMOUNT:
			int_mess = Com.Ring.TradingMobile.R.string.RET_CODE_ERROR_REQUEST_AMOUNT;
			break;
		case ERROR_INVALID_WITHDRAWAL:
			int_mess = Com.Ring.TradingMobile.R.string.RET_CODE_ERROR_INVALID_WITHDRAWAL;
			break;
		case ERROR_CANNOT_CANCEL_CASH_TRANSFER:
			int_mess = Com.Ring.TradingMobile.R.string.RET_CODE_ERROR_CANNOT_CANCEL_CASH_TRANSFER;
			break;
		case ERROR_CANNOT_CANCEL_STOCK_TRANSFER:
			int_mess = Com.Ring.TradingMobile.R.string.RET_CODE_ERROR_CANNOT_CANCEL_STOCK_TRANSFER;
			break;
		case ERROR_CANNOT_CANCEL_BUY_RIGHT:
			int_mess = Com.Ring.TradingMobile.R.string.RET_CODE_ERROR_CANNOT_CANCEL_BUY_RIGHT;
			break;
		case ERROR_CANNOT_CANCEL_ODD_LOT_ORDER:
			int_mess = Com.Ring.TradingMobile.R.string.RET_CODE_ERROR_CANNOT_CANCEL_ODD_LOT_ORDER;
			break;
		case ERROR_CANNOT_DELETE:
			int_mess = Com.Ring.TradingMobile.R.string.RET_CODE_ERROR_CANNOT_DELETE;
			break;
		case ERROR_OVER_REQUEST_CAN_BUY_RIGHT:
			int_mess = Com.Ring.TradingMobile.R.string.RET_CODE_ERROR_OVER_REQUEST_CAN_BUY_RIGHT;
			break;
		case ERROR_NOT_EXIST_BUY_RIGHT:
			int_mess = Com.Ring.TradingMobile.R.string.RET_CODE_ERROR_NOT_EXIST_BUY_RIGHT;
			break;
		case ERROR_REQUEST_VOLUME_BUY_RIGHT:
			int_mess = Com.Ring.TradingMobile.R.string.RET_CODE_ERROR_REQUEST_VOLUME_BUY_RIGHT;
			break;
		case ERROR_CANNOT_CANCEL_XRORDER:
			int_mess = Com.Ring.TradingMobile.R.string.RET_CODE_ERROR_CANNOT_CANCEL_XRORDER;
			break;
		case ERROR_NOT_EXIST_SUB_ACCOUNT:
			int_mess = Com.Ring.TradingMobile.R.string.RET_CODE_ERROR_NOT_EXIST_SUB_ACCOUNT;
			break;
		case ERROR_MAX_LENGTH:
			int_mess = Com.Ring.TradingMobile.R.string.ACCOUNTINFO_ERROR_LENGHTPASSWORD;
			break;
		case ERROR_MIN_LENGTH:
			int_mess = Com.Ring.TradingMobile.R.string.ACCOUNTINFO_ERROR_LENGHTPASSWORD;
			break;
		case PIN_LOCKED:
			int_mess = Com.Ring.TradingMobile.R.string.RET_CODE_PIN_LOCKED;
			break;
		case PIN_INACTIVED:
			int_mess = Com.Ring.TradingMobile.R.string.RET_CODE_PIN_LOCKED;
			break;
		case ERROR_MULTI_LOGIN:
			int_mess = Com.Ring.TradingMobile.R.string.ERROR_MULTI_LOGIN;
			break;
		default:
			int_mess = Com.Ring.TradingMobile.R.string.RET_CODE_SYSTEM_ERROR;
			break;
		}

		return int_mess;
	}

	public static void SetSubAccount(JSONObject objAcount, String strSubAccount) {
		JSONArray subAccountCollection;
		try {
			subAccountCollection = objAcount
					.getJSONArray("SubCustAccountCollection");
			if ((subAccountCollection != null)
					&& (subAccountCollection.length() > 0)) {
				Session.subCustAccounts.clear();
				Session.subPermissionAccount.clear();
				for (int i = 0; i < subAccountCollection.length(); i++) {
					JSONObject obj = subAccountCollection.getJSONObject(i);
					String subAccountId = obj.getString("SubCustAccountId");
					if (subAccountId.equals(strSubAccount)) {
						Session.subCustAccounts.add(subAccountId);
						Session.subPermissionAccount
								.put(subAccountId,
										obj.getJSONArray("SubCustAccountPermissionCollection"));

					}
				}
			}
		} catch (JSONException e) {
			Log.d(TAG, e.getMessage());
		}
	}
	
	public static boolean IsAvailableNetwork() {
		return WebService.IsConnected();
	}
	
	public static int SetColorStockInfo(float price, float ceil, float floor,
			float ref) {
		int color = 0;

		if (price == ceil)
			color = R.color.ceilling;
		else if (price == ref)
			color = R.color.nochange;
		else if (price == floor)
			color = R.color.floor;
		else if (price > ref)
			color = R.color.up;
		else if (price > floor)
			color = R.color.down;
		else
			color = R.color.whiles;

		return color;
	}
}
